﻿using System;
// <copyright file="Army.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System.Linq;
    using LightSwitchApplication.UserCode;

    public partial class Army
    {
        partial void SummaryGM_Compute(ref string result)
        {
            if (this.Owner != null)
            {
                result = string.Format("{0} ({1})", this.Name, this.Owner.Name);
            }
            else
            {
                result = this.Name;
            }
        }

        #region Costs

        partial void BaseRecruitmentCost_Compute(ref double result)
        {
            result = 0;

            foreach (var unit in this.LandUnits)
            {
                if (unit.BaseLandUnit != null && unit.BaseLandUnit.MusterCost.HasValue)
                {
                    result += unit.BaseLandUnit.MusterCost.Value;
                }
            }

            foreach (var unit in this.SeaUnits)
            {
                if (unit.BaseSeaUnit != null && unit.BaseSeaUnit.MusterCost.HasValue)
                {
                    result += unit.BaseSeaUnit.MusterCost.Value;
                }
            }
        }

        partial void BaseMaintenanceCost_Compute(ref double result)
        {
            result = 0;

            foreach (var unit in this.LandUnits)
            {
                if (unit.BaseLandUnit != null && unit.BaseLandUnit.MaintenanceCost.HasValue)
                {
                    result += unit.BaseLandUnit.MaintenanceCost.Value;
                }
            }

            foreach (var unit in this.SeaUnits)
            {
                if (unit.BaseSeaUnit != null && unit.BaseSeaUnit.MaintenanceCost.HasValue)
                {
                    result += unit.BaseSeaUnit.MaintenanceCost.Value;
                }
            }
        }

        #endregion

        #region Counts

        partial void NumberCharactersPublic_Compute(ref int result)
        {
            result = this.Characters.Where(c => c.Visibility == (int)VisibilityEnum.Public).Count();
        }

        partial void NumberLandUnitsPublic_Compute(ref int result)
        {
            result = this.LandUnits.Where(c => c.Visibility == (int)VisibilityEnum.Public).Count();
        }

        partial void NumberSeaUnitsPublic_Compute(ref int result)
        {
            result = this.SeaUnits.Where(c => c.Visibility == (int)VisibilityEnum.Public).Count();
        }

        partial void NumberCharactersGM_Compute(ref int result)
        {
            result = this.Characters.Count();
        }

        partial void NumberLandUnitsGM_Compute(ref int result)
        {
            result = this.LandUnits.Count();
        }

        partial void NumberSeaUnitsGM_Compute(ref int result)
        {
            result = this.SeaUnits.Count();
        }

        #endregion

        #region HasInformation

        partial void HasPublicInformation_Compute(ref bool result)
        {
            result = !string.IsNullOrEmpty(this.PublicInformation);
        }

        partial void HasGMInformation_Compute(ref bool result)
        {
            result = !string.IsNullOrEmpty(this.GMInformation);
        }

        partial void HasOwnerInformation_Compute(ref bool result)
        {
            result = !string.IsNullOrEmpty(this.OwnerInformation);
        }

        #endregion
    }
}
